﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;

namespace ADC.BL
{
    public class PromotionFactory
    {
        /// <summary>
        /// Get all visible
        /// </summary>
        /// <returns></returns>
        public static List<Promotion> GetAll()
        {
            var db = new dbADCEntities();
            return db.Promotions.Where(d => d.IsVisibled != null && d.IsVisibled == true).OrderByDescending(d => d.ModifiedDate).ToList();
        }

        public static List<Promotion> ListAll()
        {
            var db = new dbADCEntities();
            return db.Promotions.OrderByDescending(d => d.ModifiedDate).ToList();
        }

        public static Promotion GetByID(int PromotionID)
        {
            var db = new dbADCEntities();
            return db.Promotions.Where(g => g.PromotionID == PromotionID).FirstOrDefault();
        }

        // Update Programe
        /// </summary>
        /// <param name="programe"></param>
        /// <returns></returns>
        public static int Update(Promotion promotionInfo)
        {
            try
            {
                var db = new dbADCEntities();
                var objPromotion = new Promotion();
                objPromotion = db.Promotions.Where(g => g.PromotionID == promotionInfo.PromotionID).FirstOrDefault();
                objPromotion.Title = promotionInfo.Title;
                objPromotion.BodyDetails = promotionInfo.BodyDetails;
                objPromotion.Body = promotionInfo.Body;
                objPromotion.ModifiedBy = promotionInfo.ModifiedBy;
                objPromotion.ModifiedDate = DateTime.Now;
                objPromotion.IsVisibled = promotionInfo.IsVisibled;
                if (!string.IsNullOrEmpty(promotionInfo.ImagePath))
                {
                    objPromotion.ImagePath = promotionInfo.ImagePath;
                }

                return db.SaveChanges();
            }
            catch (Exception)
            {
                // Log here
                throw;
            }
        }

        public static int Delete(int promotionID)
        {
            var db = new dbADCEntities();

            // Delete Programe
            var tourInfo = db.Promotions.Where(g => g.PromotionID.Equals(promotionID)).FirstOrDefault();
            db.DeleteObject(tourInfo);
            return db.SaveChanges();
        }

        public static Promotion Insert(Promotion promotionInfo)
        {
            var db = new dbADCEntities();
            db.AddToPromotions((promotionInfo));
            db.SaveChanges();
            return promotionInfo;
        }
    }
}
